<template>
  <div id="userManagePage">
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="账号">
        <a-input
          v-model:value="searchParams.userAccount"
          placeholder="请输入账号"
          allowClear="true"
        />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input
          v-model:value="searchParams.userName"
          placeholder="请输入用户名"
          allowClear="true"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px"></div>
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="handleTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :src="record.userAvatar" :width="60" />
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 'admin'">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else-if="record.userRole === 'vip'">
            <a-tag color="orange">会员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">普通用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.key === 'action'">
          <a-button type="primary"><EditOutlined />修改</a-button>
          <a-button><DiffOutlined />详情</a-button>
          <a-button danger @click="showModal"><DeleteOutlined />删除</a-button>
          <a-modal
            v-model:open="open"
            title="删除提示"
            ok-text="确认"
            cancel-text="取消"
            @ok="deleteUser(record.id)"
          >
            <p>确认要删除吗？</p>
          </a-modal>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { deleteUserUsingPost, listUserByPageUsingPost } from '@/api/userController'
import { DeleteOutlined, EditOutlined, DiffOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { computed, onMounted, reactive, ref } from 'vue'
const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    align: 'center',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
    align: 'center',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
    align: 'center',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
    align: 'center',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
    align: 'center',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
    align: 'center',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    align: 'center',
  },
  {
    title: '操作',
    key: 'action',
  },
]
// 数据
const dataList = ref<API.UserVO[]>([])
const total = ref(0)
const open = ref<boolean>(false)

const searchParams = reactive<API.UserQueryRequest>({
  current: 1,
  pageSize: 10,
  sortField: 'createTime',
  sortOrder: 'descend',
})
// 获取用户数据
const fetchData = async () => {
  const res = await listUserByPageUsingPost({ ...searchParams })
  if (res.data.code === 200 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value ?? 0,
    showSizeChanger: true,
    showQuickJumper: true,
    showTotal: (total: number) => `共 ${total} 条数据`,
  }
})
// 切换页码
const handleTableChange = (page: any) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}
const showModal = () => {
  open.value = true
}
// 删除确认
const deleteUser = async (id: number) => {
  // 删除用户
  if (!id) {
    return
  }
  const res = await deleteUserUsingPost({ id })
  if (res.data.code === 200) {
    message.success('删除成功！')
    fetchData()
  } else {
    message.error('删除失败，' + res.data.message)
  }
  open.value = false
}
// 获取数据
const doSearch = () => {
  // 重置页码
  searchParams.current = 1
  fetchData()
}
</script>
